package com.qsl.ssyx.product.receiver;

import com.qsl.ssyx.constant.MqConst;
import com.qsl.ssyx.product.service.SkuInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.IOException;


/**
 * mq消费者-减库存
 */
public class StockReceiver {


    @Autowired
    private SkuInfoService skuInfoService;

    //   TODO 支付成功 rabbitMq发送消息②减库存，③ 删除购物车已购买的商品记录   ==>   交换机,路由key,队列  ===》 ssyx.order.direct，ssyx.minus.stock,订单号
    @RabbitListener(bindings = @QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ORDER_DIRECT, declare = "true"),
            key = {MqConst.ROUTING_MINUS_STOCK},
            value = @Queue(value = MqConst.QUEUE_MINUS_STOCK)
    ))
    public void stock(String orderNo, Message message, Channel channel) throws IOException {
        if (orderNo != null) {
            // 根据订单号减库存
            skuInfoService.minusStock(orderNo);
        }
        // ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
